package com.kl.forum.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kl.forum.dao.ArticleCommentLightMapper;
import com.kl.forum.dao.ArticleCommentMapper;
import com.kl.forum.dao.ArticleCommentReplyMapper;
import com.kl.forum.entity.Article;
import com.kl.forum.entity.ArticleComment;
import com.kl.forum.entity.ArticleCommentLight;
import com.kl.forum.entity.ArticleCommentReply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleCommentReplyService {
    @Autowired
    private ArticleCommentReplyMapper articleCommentReplyMapper;
    @Autowired
    private ArticleCommentMapper articleCommentMapper;
    @Autowired
    private ArticleCommentLightMapper articleCommentLightMapper;

    public PageInfo findArticleCommentReply(Integer userId,Integer commentId ,Integer pageNum ,Integer pageSize ){

        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("commentId",commentId);
        map.put("userId",userId);
        List list=articleCommentReplyMapper.findArticleCommentReply(map);
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    public ArticleCommentReply addArticleCommentReply(Integer commentId,String content,Integer userId,String commentName,String userName,Integer articleId, Integer commentNameId){
        ArticleCommentReply articleCommentReply = new ArticleCommentReply();
        articleCommentReply.setCommentId(commentId);
        articleCommentReply.setContent(content);
        articleCommentReply.setCreateTime(new Date());
        articleCommentReply.setUserId(userId);
        articleCommentReply.setLightNum(0);
        articleCommentReply.setCommentName(commentName);
        articleCommentReply.setUserName(userName);
        articleCommentReply.setArticleId(articleId);
        articleCommentReply.setCommentNameId(commentNameId);
        articleCommentReplyMapper.insertSelective(articleCommentReply);
        return articleCommentReply ;
    }
    public int update(ArticleCommentReply articleCommentReply){
        return articleCommentReplyMapper.updateByPrimaryKeySelective(articleCommentReply);
    }

    public  ArticleCommentReply findArticleCommentById(int id){
        return articleCommentReplyMapper.selectByPrimaryKey(id);

    }

    public Integer delArticleCommentReply(Integer replyId) {
        return articleCommentReplyMapper.deleteByPrimaryKey(replyId);
    }

    public ArticleComment findCircleArticleCommentReplyList(Integer userId, Integer commentId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map map1=new HashMap();
        map1.put("commentId",commentId);
        ArticleComment articleComment=articleCommentMapper.findOneComment(map1);
        if(userId==null){
            articleComment.setCommentLightStatus(0);
        }
        ArticleCommentLight articleCommentLight= articleCommentLightMapper.findArticleCommentLightOne(commentId,userId);
        if (articleCommentLight!=null){
            articleComment.setLightStatus(articleCommentLight.getStatus());
        }
        Map map=new HashMap();
        map.put("userId",userId);
        map.put("commentId",commentId);
        List<Map> list=articleCommentReplyMapper.findArticleCommentReplyList(map);
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        articleComment.setPageInfoReply(pageInfo);
        return articleComment;

    }
}
